package com.ruoyi.admin.train.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.train.domain.TrainPlan;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.train.mapper.adminTrainPlanMapper;
import com.ruoyi.admin.train.service.IadminTrainPlanService;

/**
 * 培训计划Service业务层处理
 * 
 * @author wusihao
 * @date 2023-03-18
 */
@Service
public class adminTrainPlanServiceImpl extends ServiceImpl<adminTrainPlanMapper, TrainPlan> implements IadminTrainPlanService
{
    @Autowired
    private adminTrainPlanMapper adminTrainPlanMapper;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询培训计划
     * 
     * @param id 培训计划主键
     * @return 培训计划
     */
    @Override
    public TrainPlan selectadminTrainPlanById(Long id)
    {
        TrainPlan trainPlan = adminTrainPlanMapper.selectadminTrainPlanById(id);
        return trainPlan;
    }

    /**
     * 查询培训计划列表
     * 
     * @param adminTrainPlan 培训计划
     * @return 培训计划
     */
    @Override
    public List<TrainPlan> selectadminTrainPlanList(TrainPlan adminTrainPlan) {
        QueryWrapper<TrainPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.eq("is_submit", 1);
        queryWrapper.eq("is_pass", 0);
        List<TrainPlan> trainPlans = adminTrainPlanMapper.selectList(queryWrapper);
        // 将dict_code转化为标签
        List<TrainPlan> res = new ArrayList<>();
        for (TrainPlan trainPlanVo1 : trainPlans) {
            TrainPlan temp = new TrainPlan();
            BeanUtils.copyProperties(trainPlanVo1, temp);
            String qualifiedType = trainPlanVo1.getQualifiedType();
            String workKind = trainPlanVo1.getWorkKind();
            String operationType = trainPlanVo1.getOperationType();
            SysDictData dictData1 = dictDataService.selectDictDataById(Long.parseLong(qualifiedType));
            SysDictData dictData2 = dictDataService.selectDictDataById(Long.parseLong(workKind));
            if (operationType != null && !operationType.equals("")) {
                SysDictData dictData3 = dictDataService.selectDictDataById(Long.parseLong(operationType));
                temp.setOperationType(dictData3.getDictLabel());
            }

            temp.setQualifiedType(dictData1.getDictLabel());
            temp.setWorkKind(dictData2.getDictLabel());

            res.add(temp);
        }
        return res;
    }



    /**
     * 修改培训计划
     * 
     * @param adminTrainPlan 培训计划
     * @return 结果
     */
    @Override
    public int updateadminTrainPlan(TrainPlan adminTrainPlan)
    {
        adminTrainPlan.setUpdateTime(DateUtils.getNowDate());
        return adminTrainPlanMapper.updateadminTrainPlan(adminTrainPlan);
    }

    @Override
    public int submitAudit(Long trainId, Long status) {
        TrainPlan trainPlan = adminTrainPlanMapper.selectadminTrainPlanById(trainId);
        trainPlan.setIsPass(status);
        trainPlan.setJingbanrenId(String.valueOf(SecurityUtils.getUserId()));
        int i = 0;
        if (trainPlan != null) {
            i = adminTrainPlanMapper.updateadminTrainPlan(trainPlan);
        }
        return i;
    }

}
